package com.pap.customer.info.agent.impl;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pap.base.service.impl.PapBaseServiceImpl;
import com.pap.base.util.date.DateUtils;
import com.pap.customer.agent.info.ICustomerInfoAgent;
import com.pap.customer.agent.info.dto.CustomerInfoWithDetailDTO;
import com.pap.customer.info.auto.entity.CustomerInfo;
import com.pap.customer.info.auto.entity.CustomerInfoRel;
import com.pap.customer.info.auto.mapper.CustomerInfoRelMapper;

@Transactional
@Service(value = "customerInfoAgent")
public class CustomerInfoAgentImpl extends PapBaseServiceImpl<CustomerInfo> implements ICustomerInfoAgent {

	@Autowired
	private CustomerInfoRelMapper customerInfoRelMapper;

	@Override
	public int saveWithDetails(CustomerInfoWithDetailDTO customerInfoWithDetailDTO, String clientLicenseId,
			String loginUserId) {
		String basciInfoId = UUID.randomUUID().toString();
		CustomerInfo basicinfo = customerInfoWithDetailDTO.getBasicinfo();
		basicinfo.setId(basciInfoId);
		basicinfo.setCreateIp("0.0.0.0");
		basicinfo.setCreateTime(DateUtils.getCurrDateTimeStr());
		basicinfo.setCreateUser(loginUserId);
		basicinfo.setClientLicenseId(clientLicenseId);
		mapper.insertSelective(basicinfo);
		
		CustomerInfoRel commoninfo = customerInfoWithDetailDTO.getCommoninfo();
		commoninfo.setId(basciInfoId + ":COMMON");
		commoninfo.setCustomerInfoId(basciInfoId);
		commoninfo.setCreateIp("0.0.0.0");
		commoninfo.setCreateTime(DateUtils.getCurrDateTimeStr());
		commoninfo.setCreateUser(loginUserId);
		commoninfo.setClientLicenseId(clientLicenseId);
		customerInfoRelMapper.insertSelective(commoninfo);
		
		CustomerInfoRel emergencyinfo = customerInfoWithDetailDTO.getEmergencyinfo();
		emergencyinfo.setId(basciInfoId + ":ENERGENCY");
		emergencyinfo.setCustomerInfoId(basciInfoId);
		emergencyinfo.setCreateIp("0.0.0.0");
		emergencyinfo.setCreateTime(DateUtils.getCurrDateTimeStr());
		emergencyinfo.setCreateUser(loginUserId);
		emergencyinfo.setClientLicenseId(clientLicenseId);
		customerInfoRelMapper.insertSelective(emergencyinfo);

		return 1;
	}

	@Override
	public int updateWithDetails(CustomerInfoWithDetailDTO customerInfoWithDetailDTO, String clientLicenseId,
			String loginUserId) {
		CustomerInfo basicinfo = customerInfoWithDetailDTO.getBasicinfo();
		basicinfo.setModifyIp("0.0.0.0");
		basicinfo.setModifyTime(DateUtils.getCurrDateTimeStr());
		basicinfo.setModifyUser(loginUserId);
		basicinfo.setClientLicenseId(clientLicenseId);
		mapper.updateByPrimaryKeySelective(basicinfo);
		
		CustomerInfoRel commoninfo = customerInfoWithDetailDTO.getCommoninfo();
		commoninfo.setModifyIp("0.0.0.0");
		commoninfo.setModifyTime(DateUtils.getCurrDateTimeStr());
		commoninfo.setModifyUser(loginUserId);
		commoninfo.setClientLicenseId(clientLicenseId);
		customerInfoRelMapper.updateByPrimaryKeySelective(commoninfo);
		
		CustomerInfoRel emergencyinfo = customerInfoWithDetailDTO.getEmergencyinfo();
		emergencyinfo.setModifyIp("0.0.0.0");
		emergencyinfo.setModifyTime(DateUtils.getCurrDateTimeStr());
		emergencyinfo.setModifyUser(loginUserId);
		emergencyinfo.setClientLicenseId(clientLicenseId);
		customerInfoRelMapper.updateByPrimaryKeySelective(emergencyinfo);
		
		return 1;
	}

	@Override
	public int deleteWithDetails(String customerBasciInfoId) {
		mapper.deleteByPrimaryKey(customerBasciInfoId);
		
		customerInfoRelMapper.deleteByPrimaryKey(customerBasciInfoId + ":COMMON");
		customerInfoRelMapper.deleteByPrimaryKey(customerBasciInfoId + ":ENERGENCY");
		
		return 1;
	}

	@Override
	public CustomerInfoWithDetailDTO selectWithDetails(String customerBasciInfoId) {
		CustomerInfo basicinfo = mapper.selectByPrimaryKey(customerBasciInfoId);
		
		CustomerInfoRel commoninfo = customerInfoRelMapper.selectByPrimaryKey(customerBasciInfoId + ":COMMON");
		CustomerInfoRel emergencyinfo = customerInfoRelMapper.selectByPrimaryKey(customerBasciInfoId + ":ENERGENCY");
		
		CustomerInfoWithDetailDTO customerInfoWithDetailDTO = new CustomerInfoWithDetailDTO();
		customerInfoWithDetailDTO.setBasicinfo(basicinfo);
		customerInfoWithDetailDTO.setCommoninfo(commoninfo);
		customerInfoWithDetailDTO.setEmergencyinfo(emergencyinfo);
		
		return customerInfoWithDetailDTO;
	}

}